//****************************************************************
//* Copyright (c) 2008 Ford Motor Company. All Rights Reserved.
//*
//* $$Workfile:   CostStructureFindForm.java  $$
//* $$Revision:   1.2  $$
//* $$Author:   TMALDON4  $$
//* $$Date:   Feb 26 2009 07:30:54  $$
//*
//*****************************************************************
package ar.com.ford.it.fullEconomics.view.form;

import java.util.Calendar;
import java.util.Collection;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.MessageResources;
import org.apache.struts.validator.ValidatorForm;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesc02Coststvers;
import ar.com.ford.it.fullEconomics.struct.CostStructureFindStruct;
import ar.com.ford.it.fullEconomics.util.FullEconomicsDateUtil;
import ar.com.ford.it.fullEconomics.wslx.FullEconomicsWslxUser;

public class CostStructureFindForm extends ValidatorForm {
    private String section;
    private String supplierTypeSel;
    private Collection supplierTypeCol;
    private String supplierCode;
    private String lookupedSupplier;
    private String statusSel;
    private Collection statusCol;
    private String findBy;
    private String commoditySel;
    private Collection commoditiesCol;
    private String partClassSel;
    private Collection partClassesCol;
    private String partPrefix;
    private String partBase;
    private String partSufix;
    private String lookupedPart;
    private String showLastApprovedOnly;
    private String dateTypeSel;
    private Collection dateTypesCol;
    private String dateMin;
    private String dateMax;
    private String userActionTypeSel;
    private Collection userActionsTypesCol;
    private String userId;
    private Collection costStructureVersions;
    private Teesc02Coststvers csVersionSelected;

    public void resetForm() {
        this.section = null;
        this.supplierTypeSel = null;
        this.supplierTypeCol = null;
        this.supplierCode = null;
        this.lookupedSupplier = null;
        this.statusSel = null;
        this.statusCol = null;
        this.findBy = null;
        this.commoditySel = null;
        this.commoditiesCol = null;
        this.partClassSel = "F";
        this.partClassesCol = null;
        this.partPrefix = null;
        this.partBase = null;
        this.partSufix = null;
        this.lookupedPart = null;
        this.showLastApprovedOnly = null;
        this.dateTypeSel = null;
        this.dateTypesCol = null;
        this.dateMin = null;
        this.dateMax = null;
        this.userActionTypeSel = null;
        this.userActionsTypesCol = null;
        this.userId = null;
        this.costStructureVersions = null;
        this.csVersionSelected = null;
    }

    public void cleanFindFields() {
        this.setSupplierTypeSel("-1");
        this.setSupplierCode(null);
        this.setStatusSel("-1");
        this.setCommoditySel("-1");
        this.setPartClassSel("-1");
        this.setPartPrefix(null);
        this.setPartBase(null);
        this.setPartSufix(null);
        this.setDateTypeSel("-1");
        this.setDateMin(null);
        this.setDateMax(null);
        this.setUserActionTypeSel("-1");
        this.setUserId(null);
        this.setShowLastApprovedOnly(null);
    }

    public Collection getCommoditiesCol() {
        return commoditiesCol;
    }

    public void setCommoditiesCol(Collection commoditiesCol) {
        this.commoditiesCol = commoditiesCol;
    }

    public String getCommoditySel() {
        return commoditySel;
    }

    public void setCommoditySel(String commoditySel) {
        this.commoditySel = commoditySel;
    }

    public String getDateMax() {
        return dateMax;
    }

    public void setDateMax(String dateMax) {
        this.dateMax = dateMax;
    }

    public String getDateMin() {
        return dateMin;
    }

    public void setDateMin(String dateMin) {
        this.dateMin = dateMin;
    }

    public Collection getDateTypesCol() {
        return dateTypesCol;
    }

    public void setDateTypesCol(Collection dateTypesCol) {
        this.dateTypesCol = dateTypesCol;
    }

    public String getDateTypeSel() {
        return dateTypeSel;
    }

    public void setDateTypeSel(String dateTypeSel) {
        this.dateTypeSel = dateTypeSel;
    }

    public String getFindBy() {
        return findBy;
    }

    public void setFindBy(String findBy) {
        this.findBy = findBy;
    }

    public String getLookupedSupplier() {
        return lookupedSupplier;
    }

    public void setLookupedSupplier(String lookupedSupplier) {
        this.lookupedSupplier = lookupedSupplier;
    }

    public Collection getPartClassesCol() {
        return partClassesCol;
    }

    public void setPartClassesCol(Collection partClassesCol) {
        this.partClassesCol = partClassesCol;
    }

    public String getPartClassSel() {
        return partClassSel;
    }

    public void setPartClassSel(String partClassSel) {
        this.partClassSel = partClassSel;
    }

    public String getSection() {
        return section;
    }

    public void setSection(String section) {
        this.section = section;
    }

    public String getShowLastApprovedOnly() {
        return showLastApprovedOnly;
    }

    public void setShowLastApprovedOnly(String showLastApprovedOnly) {
        this.showLastApprovedOnly = showLastApprovedOnly;
    }

    public Collection getStatusCol() {
        return statusCol;
    }

    public void setStatusCol(Collection statusCol) {
        this.statusCol = statusCol;
    }

    public String getStatusSel() {
        return statusSel;
    }

    public void setStatusSel(String statusSel) {
        this.statusSel = statusSel;
    }

    public Collection getSupplierTypeCol() {
        return supplierTypeCol;
    }

    public void setSupplierTypeCol(Collection supplierTypeCol) {
        this.supplierTypeCol = supplierTypeCol;
    }

    public String getSupplierTypeSel() {
        return supplierTypeSel;
    }

    public void setSupplierTypeSel(String supplierTypeSel) {
        this.supplierTypeSel = supplierTypeSel;
    }

    public Collection getUserActionsTypesCol() {
        return userActionsTypesCol;
    }

    public void setUserActionsTypesCol(Collection userActionsTypesCol) {
        this.userActionsTypesCol = userActionsTypesCol;
    }

    public String getUserActionTypeSel() {
        return userActionTypeSel;
    }

    public void setUserActionTypeSel(String userActionTypeSel) {
        this.userActionTypeSel = userActionTypeSel;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public Collection getCostStructureVersions() {
        return costStructureVersions;
    }

    public void setCostStructureVersions(Collection costStructureVersions) {
        this.costStructureVersions = costStructureVersions;
    }

    public String getPartBase() {
        return partBase;
    }

    public void setPartBase(String partBase) {
        this.partBase = partBase;
    }

    public String getPartPrefix() {
        return partPrefix;
    }

    public void setPartPrefix(String partPrefix) {
        this.partPrefix = partPrefix;
    }

    public String getPartSufix() {
        return partSufix;
    }

    public void setPartSufix(String partSufix) {
        this.partSufix = partSufix;
    }

    public CostStructureFindStruct getFindParameters(FullEconomicsWslxUser user) {
        CostStructureFindStruct parameters = new CostStructureFindStruct();

        parameters.setPlant(user.getTeesg01Plant());
        parameters.setSupplierClass(this.getSupplierTypeSel());
        parameters.setSupplierCode(this.getSupplierCode());
        parameters.setStatus(this.getStatusSel());
        parameters.setCommodityNumber(this.getCommoditySel());
        parameters.setLastAppOnly(this.getShowLastApprovedOnly());
        parameters.setPartClass(this.getPartClassSel());
        parameters.setPartPrefix(this.getPartPrefix());
        parameters.setPartBase(this.getPartBase());
        parameters.setPartSufix(this.getPartSufix());
        parameters.setDateType(this.getDateTypeSel());
        parameters.setDateMin(this.getDateMin());
        parameters.setDateMax(this.getDateMax());
        parameters.setUserActionType(this.getUserActionTypeSel());
        parameters.setUserId(this.getUserId());
        return parameters;
    }

    public String getSupplierCode() {
        return supplierCode;
    }

    public void setSupplierCode(String supplierCode) {
        this.supplierCode = supplierCode;
    }

    public boolean getSelecMapped(String key) {
        return false;
    }

    public void setSelecMapped(String key, String value) {
        this
                .setCsVersionSelected((Teesc02Coststvers) ((List) costStructureVersions)
                        .get(Integer.parseInt(key) - 1));
    }

    public Teesc02Coststvers getCsVersionSelected() {
        return csVersionSelected;
    }

    public void setCsVersionSelected(Teesc02Coststvers csVersionSelected) {
        this.csVersionSelected = csVersionSelected;
    }

    public void setCsVersionMapped(String key, String value) {
        this
                .setCsVersionSelected((Teesc02Coststvers) ((List) costStructureVersions)
                        .get(Integer.parseInt(key) - 1));
    }

    public String getLookupedPart() {
        return lookupedPart;
    }

    public void setLookupedPart(String lookupedPart) {
        this.lookupedPart = lookupedPart;
    }

    public void reset(ActionMapping mapping, HttpServletRequest request) {
        this.showLastApprovedOnly = null;
    }

    /**
     * 
     */
    public ActionErrors validate(ActionMapping mapping,
                                 HttpServletRequest request) {
        ActionErrors ae = new ActionErrors();
        MessageResources resources = (MessageResources) request
                .getAttribute(org.apache.struts.Globals.MESSAGES_KEY);
        Calendar calMin = null;
        Calendar calMax = null;

        if (StringUtils.isNotBlank(this.getDateMin())) {
            calMin = FullEconomicsDateUtil.format(this.getDateMin());
            if (calMin == null) {
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                        "errors.date", this.getDateMin()));
            }
        }

        if (StringUtils.isNotBlank(this.getDateMax())) {
            calMax = FullEconomicsDateUtil.format(this.getDateMax());
            if (calMax == null) {
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                        "errors.date", this.getDateMax()));
            }
        }

        if (calMin != null && calMax != null) {
            if (!calMin.before(calMax)) {
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                        "costStructure.find.error.dateMinGTDateMax"));
            }
        }

        if ("newVersion".equalsIgnoreCase(this.getSection())
                || "copyTo".equalsIgnoreCase(this.getSection())) {
            if (this.getCsVersionSelected() == null) {
                ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                        "error.noSelected.m", resources
                                .getMessage("costStructure.p")));
            }
        }

        return ae;
    }

    public String getSelectedIndex() {
        return "false";
    }

    public void setSelectedIndex(String selectedIndex) {
        this
                .setCsVersionSelected((Teesc02Coststvers) ((List) costStructureVersions)
                        .get(Integer.parseInt(selectedIndex) - 1));
    }

}
